package top.hzy520;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 9:32 2018/7/26
 */
public class Main {
    private static double coutPrice=0;
    private static List<User> userList=new ArrayList<>();
    private static List<Goods> goods=new ArrayList<>();
    private static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
          initData();
          User user=new User();

        System.out.println("请输入账号");
          user.setUsername(scanner.next());
        System.out.println("请输入密码");
        user.setPassword(scanner.next());
          login(user);
          purchase(user);
    }
    public static void initData(){
        userList.add(new User("houzeyu","123456"));
        goods.add(new Goods("足球",110.5,20,"体育用品"));
        goods.add(new Goods("篮球",130.5,10,"体育用品"));
        goods.add(new Goods("羽毛球",300.5,20,"体育用品"));
        goods.add(new Goods("乒乓球",2.5,20,"体育用品"));
    }
    public static void initGoodsList(){
        System.out.println("********************************************");
        System.out.println("商品名字\t"+"商品价格\t"+"商品库存\t"+"商品分类\t");
        goods.stream().forEach(param->{
            System.out.println(param.getName()+"\t \t"+param.getPrice()+"\t\t"+param.getCount()+"\t \t \t"+param.getType()+"\t");
        });
    }
    public static void login(User user){
       for (User user1: userList){
            if (user1.getUsername().equals(user.getUsername())&&user1.getPassword().equals(user.getPassword())){
                System.out.println("登录成功");
                initGoodsList();
            }
       }
    }
    public static boolean validateGoodsname(String goodsname){
        for(Goods good:goods){
               if (goodsname.equals(good.getName())){
                   return true;
               }
        }
        System.out.println("您输入商品的商品我们没有");
        return false;
    }
    public static boolean validateGoodsCount(String goodsname ,int goodsCount){
        for(Goods good:goods){
            if (goodsname.equals(good.getName())){
                if (good.getCount()>=goodsCount){
                    return true;
                }
            }
        }
        System.out.println("该商品库存不足");
        return false;
    }
    public static void purchase(User user){
        Goods purchaseGoods=new Goods();
        boolean lock=true;
        do {
            System.out.println("请输入您要购买的商品名字");
            String goodsname= scanner.next();
           boolean goodsnameValidate=validateGoodsname(goodsname);
           if (goodsnameValidate){
               purchaseGoods.setName(goodsname);
               System.out.println("请您输入要购买的商品数量");
               int goodsCount=scanner.nextInt();
               if (validateGoodsCount(goodsname,goodsCount)){
                   purchaseGoods.setCount(goodsCount);
                   lock=false;
               }
           }
        }while (lock);
        goods.stream().forEach(param->{
            if (purchaseGoods.getName().equals(param.getName())){
                param.setCount(param.getCount()-purchaseGoods.getCount());
                   user.setScore((int) (user.getScore()+param.getPrice()*purchaseGoods.getCount()));
                   goods.set(goods.indexOf(param),param);
                   coutPrice=coutPrice+param.getPrice()*purchaseGoods.getCount();
            }
        });
        System.out.println("您还需要购买别的商品吗 Y/N");
        String answer=scanner.next();
        if (answer.equals("Y")){
            purchase(user);
        }else {
            System.out.println("您本次总共消费:"+coutPrice);
            System.out.println("请您付款");
            double money=scanner.nextDouble();

            if (user.getScore()>=10000){
                System.out.println("恭喜您获得一次抽奖机会");
               Random random=new Random();
               int sales=random.nextInt(9)+1;
                System.out.println("恭喜您获得"+sales+"折优惠");
               double   getmoney=( ((double) (sales*10))/100);
                System.out.println("实收:￥" +money +"\t优惠金额:￥"+(coutPrice-(coutPrice*getmoney))+ "\t应收: ￥ "+  getmoney*coutPrice + "\t找零:￥" +(money-(getmoney*coutPrice)));
                System.out.println("本次消费增加积分为:"+(int)coutPrice);
                System.out.println("当前个人积分为: " +user.getScore());
            }
            System.out.println("欢迎您下次光临");
        }
    }
}